package com.pocket.mic.common.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class TimeUtil {
	
	private static final Logger logger = LoggerFactory.getLogger(TimeUtil.class);
	
	public static final String FORMAT_YMD = "yyyy-MM-dd";
	public static final String FORMAT_YMDHMS = "yyyy-MM-dd HH:mm:ss";
	public static final String FORMAT_YMDHMS_SSS = "yyyy-MM-dd HH:mm:ss.SSS";
	
	public final static int SUNDAY = Calendar.SUNDAY;
    public final static int MONDAY = Calendar.MONDAY;
    public final static int TUESDAY = Calendar.TUESDAY;
    public final static int WEDNESDAY = Calendar.WEDNESDAY;
    public final static int THURSDAY = Calendar.THURSDAY;
    public final static int FRIDAY = Calendar.FRIDAY;
    public final static int SATURDAY = Calendar.SATURDAY;

	public static String getDate(Date date, String dateFormatStr){
		SimpleDateFormat sdf  = new SimpleDateFormat(dateFormatStr);
		String  dateStr = sdf.format(date);
		return dateStr;
	}
	
	/**
	 *
     *
	 * @param dateStr
	 * @param dateFormatStr
	 * @return
	 */
	@SuppressWarnings("finally")
	public static Date getPreWeekSunday(String dateStr, String dateFormatStr){
		Date date = null;
		try {
			date = getPreWeekDay(dateStr, TimeUtil.SUNDAY, dateFormatStr);
		} catch (ParseException e) {
			logger.error(e.getStackTrace().toString());
		}finally{
			return date;
		}
	}
	
	/**
	 * 获取上周日期
     *
	 * @param pre_day_of_week
	 * @param dateStr
	 * @return
	 * @throws java.text.ParseException
	 */
	public static Date getPreWeekDay(String dateStr, int pre_day_of_week, String dateFormatStr) throws ParseException {
		
		Calendar cal = getCalendar(dateStr, dateFormatStr);
		
		cal.add(Calendar.DATE, pre_day_of_week - cal.get(Calendar.DAY_OF_WEEK));
		
		return cal.getTime();
	}
	
	public static Date getDate(String dateStr, String dateFormatStr) throws ParseException{
	
		SimpleDateFormat sdf  = new SimpleDateFormat(dateFormatStr);
		Date  date = sdf.parse(dateStr);
		
		return date;
	}
	
	public static Calendar getCalendar(String dateStr, String dateFormatStr) throws ParseException{
		
		Calendar cal=Calendar.getInstance();
		cal.setTime(getDate(dateStr, dateFormatStr));
		
		return cal;
	}

}
